package com.lishe.lishe.lishe.module.me.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.auth.fragment.UserProtocolFragment;
import com.lishe.lishe.lishe.module.excitation.bean.user.UserObject;
import com.lishe.lishe.lishe.module.main.activity.DownloadManagerReceiver;
import com.lishe.lishe.lishe.module.main.activity.MainActivity0;
import com.lishe.lishe.lishe.utils.AppUtils;
import com.lishe.lishe.lishe.utils.PermissionUtils;
import com.lishe.lishe.lishe.utils.SharedPreferencesUtils;
import com.lishe.lishe.lishe.widget.CircleImageView;

/**
 * Created by yang on 2017/11/20.
 * 关于礼舍
 */

public class AboutUpdateActivity extends BaseActivity implements View.OnClickListener {

    public static void jump(Activity activity) {
        Intent intent = new Intent(activity, AboutUpdateActivity.class);
        activity.startActivity(intent);
    }

    private TextView mTextCurrentVersion,
            mTextUpdate,
            mTextTitle;

    private Button mButtonBack;
    private LinearLayout mLinearService, mLinearPingfen,mLinearUpdate;

    private long downloadId=-1;
    private DownloadManagerReceiver receiver;//下载广播监听

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_update);
        initView();
        setListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterDownloadReceiver();
    }

    private void initView() {
        mTextCurrentVersion = findViewById(R.id.current_version);
        mButtonBack = findViewById(R.id.title_left_button);
        mTextTitle = findViewById(R.id.title_text_title);
        mTextUpdate = findViewById(R.id.update);
        mLinearService = findViewById(R.id.ll_service);
        mLinearPingfen = findViewById(R.id.ll_pingfen);
        mLinearUpdate = findViewById(R.id.ll_update);

        mTextTitle.setText("关于礼舍");
        int app_code= SharedPreferencesUtils.getAppCode();
        mTextCurrentVersion.setText("V" + AppUtils.getVersionName(this));
        if(app_code>AppUtils.getVersionCode(this)){
            mLinearUpdate.setVisibility(View.VISIBLE);
        }else{
            mLinearUpdate.setVisibility(View.GONE);
        }
    }

    private void setListener() {
        mButtonBack.setOnClickListener(this);
        mLinearPingfen.setOnClickListener(this);
        mLinearService.setOnClickListener(this);
        mTextUpdate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_button:
                finish();
                break;
            case R.id.ll_service://礼舍服务协议
                getSupportFragmentManager().beginTransaction().replace(R.id.container, UserProtocolFragment.newInstance())
                        .addToBackStack("UserProtocolFragment").commit();
                break;
            case R.id.ll_pingfen://评分
                break;
            case R.id.update:
                registerDownloadReceiver();
                if (PermissionUtils.isStorageWritePermission(AboutUpdateActivity.this) && downloadId == -1) {
                    downloadId = AppUtils.downLoadApkForDownloadManager(AboutUpdateActivity.this, AppUtils.APK_DOWNLOAD_URL);
                }
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        /**
         * grantResults 返回-1，表示请求权限失败，返回0表示请求权限成功
         */
        boolean success = true;
        for (int i : grantResults) {
            if (i != 0) {
                success = false;
                break;
            }
        }
        if (success) {
            if (requestCode == PermissionUtils.request_WRITE_EXTERNAL_STORAGE && downloadId == -1) {
                downloadId = AppUtils.downLoadApkForDownloadManager(AboutUpdateActivity.this, AppUtils.APK_DOWNLOAD_URL);
            }
        } else {
            toast(getString(R.string.get_sdcard_permission_fail_please_to_get_permission_for_setting));
        }
    }

    private void registerDownloadReceiver() {
        if (receiver == null) {
            receiver = new DownloadManagerReceiver();
        }
        IntentFilter filter = new IntentFilter();
        //点击通知action
        filter.addAction("android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED");
        //下载完成action
        filter.addAction("android.intent.action.DOWNLOAD_COMPLETE");
        registerReceiver(receiver, filter);
    }

    private void unregisterDownloadReceiver() {
        if (receiver != null) unregisterReceiver(receiver);
    }
}
